<?php 
	require_once '../libs/required.php';
	require_once '../DbTables/quiz.class.php';
	require_once '../DbTables/category.class.php';
	
	if(empty($_SESSION['user']) || $_SESSION['user']->user_roles != ADMIN)
	{
		header('Location: ../login.php');
	}
	
	if(isset($_GET['action']))
		$action =  $_GET['action'];
	else
		$action = '';
	
	switch ($action) {
		case 'edit':
			$id = $_GET['id'];
		    $o_Quiz = $o_QuizModel->o_fGetQuizById($id);
			//Get list category
			$o_ListCate = $o_CategoryModel->o_fListcategories();			
			break;
		case 'add':
			//Get list category
			$o_ListCate = $o_CategoryModel->o_fListcategories();
			if($_POST)
			{
				add_action($o_QuizModel,$_POST['cat_id']);
				header('Location: question.php');
			}  
			break;
		case 'update':
				$id = $_POST['id'];
				$question = trim($_POST['question']);
				$answer = trim($_POST['answer']);
				$cat_id = $_POST['cat_id'];
				$a_Quiz =array($question,$answer,$cat_id,$id);
		       	$result = $o_QuizModel->b_fUpdateQuiz($a_Quiz);
		       	if($result)
		       		header('Location: question.php');
			break;
		case 'delete':
		       $id = $_GET['id'];
			   $result = $o_QuizModel->o_fDeleteQuizById($id);
			   	if($result)
		       		header('Location: question.php');
			break;
		default:
			$o_ListQuiz = $o_QuizModel->o_fGetListQuiz();	
			break;
	}
	
	
function add_action($o_QuizModel,$cat_id)
{
	if(isset($_FILES['csv'])) {
		if($_FILES['csv']['size'] > 0) {
			//get the csv file
			$file = $_FILES['csv'][tmp_name];
			$handle = fopen($file,"r");
			//loop through the csv file and insert into database
			do {
				if ($data[0]) {	
					$a_Data = array(trim($data[0]),trim($data[1]),$cat_id);		
					$o_QuizModel->b_fInsertQuiz($a_Data);
				}
			} while ($data = fgetcsv($handle,1000,";","'"));
		}
	}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Question</title>
	<meta HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8" />
    <script src="../scripts/jquery-1.9.1.min.js"></script>
    <script src="../scripts/bootstrap.min.js"></script>
    <script src="../scripts/jquery.validate.min.js"></script>
    <link href="../styles/bootstrap.css" rel="stylesheet" />
    <link href="../styles/site.css" rel="stylesheet" />
    <link href="../styles/admin.css" rel="stylesheet" />
    <link href="../styles/img/favicon.ico" rel="shortcut icon" />
</head>
<body>
   <div class="container">
   	  <div id="sidebar">
   	  		<ul>
   	  			<li><a href="question.php">Questions</a></li>
   	  			<li><a href="category.php">Categories</a></li>
   	  			<li><a href="../logout.php">Logout</a></li>
   	  		</ul>
	  </div>
   	  <div id="main">
   	  		<?php if($action ==''){?>
   	  		<h3 class="form-signin-heading">Questions management</h3>
   	  		<div><a href="question.php?action=add">Add question</a></div>
   	  		<table>
   	  			<tr>
   	  				<td class="first">Category</td>
   	  				<td class="first">Question</td>
   	  				<td class="first">Answer</td>
   	  				<td class="end">Action</td>
   	  			</tr>
   	  			<?php if($o_ListQuiz){ foreach($o_ListQuiz as $a_Quiz){?>
   	  			<tr>
   	  				<td><?php echo $a_Quiz['name'];?></td>
   	  				<td><?php echo $a_Quiz['question'];?></td>
   	  				<td><?php echo $a_Quiz['answer'];?></td>
   	  				<td>
   	  				 <a href="question.php?action=edit&id=<?php echo $a_Quiz['id'];?>"><img class="icon" src="img/icon_edit.gif" alt="Edit Question" title="Edit Question" /></a>
   	  				 <a href="question.php?action=delete&id=<?php echo $a_Quiz['id'];?>"><img class="icon" src="img/icon_delete.gif" alt="Delete Question" title="Delete Question" /></a>
   	  				</td>
   	  			</tr>
   	  			<?php }}?>
   	  		</table>
   	  		<?php }?>
   	  		
   	  		<?php if($action =='add'){?>
	   	  		<h3 class="form-signin-heading">Import questions</h3>
	   	  		<form class="form-signin" action="question.php?action=add" method="POST" id="input_form" enctype="multipart/form-data">
		   	  		<input type="hidden" name="upload" value="upload">
		   	  		<input name="csv"  type="file" id="csv" style="width:580px" />
		   	  		<br/>
		   	  		<select name ="cat_id">
		   	  			<?php foreach ($o_ListCate as $a_Cate){?>
		   	  				<option value="<?php echo $a_Cate['id'];?>"><?php echo $a_Cate['name'];?></option>	
		   	  			<?php }?>
		   	  		</select>
		   	  		<button type="submit" class="btn btn-large btn-primary btn-success">Upload</button>
	   	  		</form>
   	  		<?php }?>
   	  		<?php if($action =='edit'){?>
	   	  		<h3 class="form-signin-heading">Edit questions</h3>
	   	  		<form class="form-signin" action="question.php?action=update" method="POST" id="input_form" enctype="multipart/form-data">
		   	  		<input type="hidden" name="id" value="<?php echo $o_Quiz->quiz_id ?>">
		   	  		<input type="text" name="question" value="<?php echo $o_Quiz->quiz_question ?>">
		   	  		<input name="answer"  type="text" id="answer" value="<?php echo $o_Quiz->quiz_answer?>"/>		   	  		
		   	  		<select name ="cat_id">
		   	  			<?php foreach ($o_ListCate as $a_Cate){?>
		   	  				<option value="<?php echo $a_Cate['id'];?>" <?php if($o_Quiz->quiz_cat_id == $a_Cate['id']) echo "selected ='selected'"?>><?php echo $a_Cate['name'];?></option>	
		   	  			<?php }?>
		   	  		</select>
		   	  		<button type="submit" class="btn btn-large btn-primary btn-success">Update</button>
	   	  		</form>
   	  		<?php }?>
   	  </div>
   	  <div class="cl"></div>
    </div>
</body>
</html>
<?php
ob_flush();
?>